<?php

class PerformerController extends ModuleController {

    const PER_PAGE = 10;

    private $current_page = 0;

    private $comments_obj;
    
    public function onCreatePage() { 
         $this->model = new PerformerModel();
         if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>=0)
         {
             $this->current_page = $_GET['page'];
         }
         
         $this->comments_obj = ModuleFactory::create("commentscontroller", $this->getContext());
         $this->form_manager->addHandler($this->comments_obj);
     }
     
     
     
     /**
      * Выполнить задание
      * @param type $args
      */
     public function do_perform_arguments_checker() {
         $args = $this->getArgs();
         $args[1] = intval($args[1]);
         if(!$this->model->hasTaskInBase($args[1])) return FAIL;
         return OK;
     }
    /**
     * @blocks: navigation, loginblock, vklogin
     */
     public function do_perform() {
        $args = $this->getArgs(); 
        $task_id = $args[1];

        $this->load->library('class.Task.php', 'performer');
        $this->load->library('answer_type', 'performer');
        
        $task = Task::load($task_id, $this->getContext());

        $answer_type = $task->getAnswerType();
        $params = array('data' => (array) $task, 'answer_type'=>$answer_type->getName());
        $html_body = $this->view->getPerformer_perform($params);
        $this->output($html_body);        
        $this->output($answer_type->getFormHTML());
        return OK;
    }

     
     public function task_page_arguments_checker() {
         $args = $this->getArgs();
         if(!is_numeric($args[1])) return FAIL;
         $args[1] = intval($args[1]);
         return OK;
     }
    /**
     * @blocks: navigation, loginblock, vklogin
     */
     public function task_page() {
         $args = $this->getArgs();
         
         $task_id = $args[1];
         $task_data = $this->model->getTask($task_id);
         if($task_data === FALSE) return NOT_FOUND;
         $task_data['creator'] = $this->user[$task_data['user_id']];
         
         $params = array('task_data'=>$task_data);
         $html_body = $this->view->getPerformer_taskpage($params);
         
         $this->output($html_body);
         
         
         $this->comments_obj->printCommentsList();
         $this->comments_obj->printCommentsInputBlock();
         
         return OK;
     }
    /**
     * @blocks: navigation, loginblock, vklogin
     */
     public function task_table() {
       
         $total = $this->model->getTotalPages(self::PER_PAGE);
         $params = array('total'=>$total);
         $html_pager = $this->view->getPager($params);
         $tasks = $this->model->getTasks($this->current_page, self::PER_PAGE);
         
         foreach ($tasks as $key => $value) {
             $tasks[$key]['creator'] = $this->user[$tasks[$key]['user_id']];
         }
         $params = array('tasks'=>$tasks);
         $html_body = $this->view->getPerformer_tasklist($params);
         
         $this->output($html_pager);
         $this->output($html_body);
         $this->output($html_pager);
         
         return OK;
     }
}
?>
